
//############Movement gait||运动步态
enum gait {
    //% block="Trot"
    3,
    //% block="F_TROT"
    4,
    //% block="S_TROT"
    5,
    //% block="Crawl"
    6
    
      
}

//############Actions||动作组
enum Choice{
    //% block="0"
    0
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="3"
    3,
    //% block="4"
    4,
    //% block="5"
    5,
    //% block="6"
    6,
    //% block="7"
    7,
    //% block="8"
    8,
    //% block="9"
    9
    
}

//############Actions state||动作组状态
enum Actions{
    //% block="Enable"
    1,
    //% block="Not_Enable"
    0

}


//############Joint_Control||腿的位置
enum Select1{
    //% block="Left_front_leg"
    0,
    //% block="Right_front_leg"
    1,
    //% block="Left_hind_leg"
    2,
    //% block="Right_hind_leg"
    3,
    //% block="not_select1"
    4
}

//############Joint_Control||关节位置
enum Select2{
    //% block="Thigh"
    0,
    //% block="Shank"
    1,
    //% block="Hip"
    2,
    //% block="not_select2"
    3
}

 //############Movement direction||运动方向
enum Mov_dir{
    //% block="Forward"
    0,
    //% block="Backward"
    1,
    //% block="Turn_left"
    2,
    //% block="Turn_right"
    3,
    //% block="Shift_left"
    4,
    //% block="Shift_right"
    5
}

//############Movement Angle||运动角度
enum Mov_ang{
    //% block="Left_swing"
    0,
    //% block="Right_swing"
    1,
    //% block="Look_down"
    2,
    //% block="Look_up"
    3,
    //% block="Yaw_left"
    4,
    //% block="Yaw_right"
    5
}

//############gesture||手势
enum gesture{
    //% block="Right to left"
    1,
    //% block="From left to right"
    2,
    //% block="From top to bottom"
    4,
    //% block="Bottom up"
    8,
    //% block="Back to front"
    16,
    //% block="From front to back"
    32,
    //% block="Clockwise"
    64,
    //% block="Counterclockwise"
    128,
    //% block="Wave"
    256

}

//############Audio control instructions||音频控制指令
enum CMD{
    //% block="back"
    0x01,
    //% block="next"
    0x02,
    //% block="Specifies the track"
    0x03,
    //% block="volume+"
    0x04,
    //% block="volume-"
    0x05,
    //% block="Play"
    0x0D,
    //% block="Pause playback"
    0x0E,
    //% block="Stop playback"
    0x16,
    //% block="The single plays on a loop"
    0x19,
    // //% block="Specifies the volume"
    // 0x06,
    //% block="Loop playback"
    0x08,
    //% block="Enter sleep mode"
    0x0A,
    //% block="Wake up to sleep"
    0x0B,
    //% block="Chip reset"
    0x0C
    

}

//############track||曲目
enum dat{
    //% block="none"
    0x00,
    //% block="welcome"
    0x01,
    //% block="thank"
    0x02,
    //% block="song"
    0x03

}
//############mechanical||机械臂
enum ARM{
    //% block="Rotate"
    0,
    //% block="BOOM"
    1,
    //% block="Forearm"
    2,
    //% block="Clip"
    3,	
	//% block="Whole"
    254,
}

//% color="#0fbc11" iconWidth=60 iconHeight=50
namespace Quadruped {
	Generator.addImport("import Robots"); 
    //###Start||初始化
    //% block="initialization" blockType="command" 
    export function SPI_Init(parameter: any, block: any) {
       //Generator.addImport("import Robots"); 
       Generator.addCode(`Robots.Init()`); 
   }

    //###Start||启动
    //% block="Start" blockType="command"
    export function Start(parameter: any, block: any) {
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Mode(0x02)`);
        //Generator.addCode(`time.sleep_ms(100);`);
    }

    //###Stop||停止
    //% block="Stop" blockType="command"
    export function Stop(block: any): void {
        //Generator.addImport("import Robots");
        //Generator.addCode(`Robots.Robot_Reset();`);
        //Generator.addCode(`time.sleep_ms(1000);`);
        Generator.addCode(`Robots.Mode(0x07)`);
        Generator.addCode(`time.sleep_ms(1000)`);
        Generator.addCode(`Robots.Mode(0x01)`);
    }
    
    //####Reset||复位
    //% block="Reset" blockType="command"
    export function Reset(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Reset()`);

    }

    //###Stand||站立 
    //% block="Stand" blockType="command"
    export function Stand(): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Mode(0x07)`);
    }

    //####Hight||站立的高度
    //% block="Standing height[H]" blockType="command"
    //%H.shadow="range" H.params.min=0 H.params.max=10 H.defl=10
    export function Height(parameter: any){
        let h = parameter.H.code;
        //Generator.addImport("import Robots");
        //Generator.addCode(`Robots.Mode(0x07)`);
        Generator.addCode(`Robots.High(${h})`); 
       // Generator.addCode(`time.sleep_ms(1700);`);
    }

    //###Gait||步态
    //% block="Gait[G]" blockType="command"
    //% G.shadow="dropdown" G.options="gait" G.defl="gait.Trot"
    export function Gait(parameter: any) {
        let g = parameter.G.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Mode(${g})`);
    }

        //###Before and after movement direction and speed||前后运动方向与速度
    //% block="Control[FOR]Speed[V1]" blockType="command"
    //% FOR.shadow="dropdown" FOR.options="Mov_dir" FOR.defl="Mov_dir.0"
    //% V1.shadow="range" V1.params.min=0 V1.params.max=14 V1.defl=0
    export function movement(parameter: any, block: any) {
        let ffor = parameter.FOR.code;    
        let v1 = parameter.V1.code;
       
        //Generator.addImport("import Robots");
        if(ffor == 0){
        Generator.addCode(`Robots.Xspeed(${v1/10})`);
        }
        else if(ffor == 1){
            Generator.addCode(`Robots.Xspeed(${v1/(-10)})`);
        }
        else if(ffor == 2){
            Generator.addCode(`Robots.Zspeed(${v1/10})`);
        }
        else if(ffor == 3){
            Generator.addCode(`Robots.Zspeed(-${v1/10})`);
        }
        else if(ffor == 4){
            Generator.addCode(`Robots.Yspeed(-${v1/10})`);
        }
        else if(ffor == 5){
            Generator.addCode(`Robots.Yspeed(${v1/10})`);
        }
   }

    //###Control angle||控制角度
    //% block="Control[SWAY]angle[ANGLE1]" blockType="command"
    //% SWAY.shadow="dropdown" SWAY.options="Mov_ang" SWAY.defl="Mov_ang.0"
    //% ANGLE1.shadow="range" ANGLE1.params.min=0 ANGLE1.params.max=14 ANGLE1.defl=0
    export function Control_angle(parameter: any, block: any): void{
        let sway = parameter.SWAY.code;    
        let angle1 = parameter.ANGLE1.code;
 
        //Generator.addImport("import Robots");
        if(sway == 0){
        Generator.addCode(`Robots.Yangle(${angle1/10})`);
        }
        else if(sway == 1){
            Generator.addCode(`Robots.Yangle(-${angle1/10})`);
        }
        else if(sway == 2){
            Generator.addCode(`Robots.Xangle(${angle1/10})`);
        }
        else if(sway == 3){
            Generator.addCode(`Robots.Xangle(-${angle1/10})`);
        }
        else if(sway == 4){
            Generator.addCode(`Robots.Zangle(${angle1/10})`);
        }
        else if(sway == 5){
            Generator.addCode(`Robots.Zangle(-${angle1/10})`);
        }
    }

    //###Action group||动作组
    //% block="Action_group[GROUP]State[STATE]" blockType="command"
    //% GROUP.shadow="dropdown" GROUP.options="Choice" GROUP.defl="Choice.Enable"
    //% STATE.shadow="dropdown" STATE.options="Actions" STATE.defl="Actions.Enable"
    export function Action(parameter: any, block: any): void{
        let group = parameter.GROUP.code;
        let state = parameter.STATE.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Action(${group},${state})`); 
    }

    //####Action_On||开启动作编辑
    //% block="Action_On" blockType="command"
    export function Action_On(): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Action_On()`);
    }

    //###Joint_Control||存储关节角度
    //% block="Joint_Control_Leg[LEG]joint[JOINT]angle[ANGLE]" blockType="command"
    //% LEG.shadow="dropdown" LEG.options="Select1" LEG.defl="Select1.4"
    //% JOINT.shadow="dropdown" JOINT.options="Select2" JOINT.defl="Select2.3"
    //% ANGLE.shadow="range" ANGLE.params.min=-90 ANGLE.params.max=90 ANGLE.defl=0
    export function Joint_Control(parameter: any, block: any): void{
        let leg = parameter.LEG.code;
        let joint = parameter.JOINT.code;
        let angle = parameter.ANGLE.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Joint_Control(${leg},${joint},${angle})`);
    }

    //###Joint_Control||存储关节角度
    //% block="Joint_Control_Leg[LEG]angle[ANGLE]angle1[ANGLE1]angle2[ANGLE2]" blockType="command"
    //% LEG.shadow="dropdown" LEG.options="Select1" LEG.defl="Select1.4"
    //% ANGLE.shadow="range" ANGLE.params.min=0 ANGLE.params.max=120 ANGLE.defl=45
    //% ANGLE1.shadow="range" ANGLE1.params.min=0 ANGLE1.params.max=120 ANGLE1.defl=90
    //% ANGLE2.shadow="range" ANGLE2.params.min=-20 ANGLE2.params.max=20 ANGLE2.defl=0
    export function Joint_Controls(parameter: any, block: any): void{
        let leg = parameter.LEG.code;
        //let joint = parameter.JOINT.code;
        let angle = parameter.ANGLE.code;
        let angle1 = parameter.ANGLE1.code;
        let angle2 = parameter.ANGLE2.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Joint_Controls(${leg},${angle},${angle1},${angle2})`);
    }

    //####Action_Execution||设置执行步骤耗时
    //% block="Action_Execution[AE]" blockType="command"
    //%AE.shadow="range" AE.params.min=0.00 AE.params.max=10.00 AE.defl=0.00
    export function Action_Execution(parameter: any, block: any){
        let time = parameter.AE.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Action_Execution(${time})`);
    }

    //####Action_Off||关闭动作编辑
    //% block="Action_Off" blockType="command"
    export function Action_Off(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Action_Off()`);
    }

    // //###Steering gear control||舵机控制
    // //% block="Steering_gear_control the[ge]PWM[num]Speed[v2]millisecond" blockType="command"
    // //% ge.shadow="dropdown" ge.options="PWM" ge.defl="PWM.0"
    // //% num.shadow="range" num.params.min=0 num.params.max=2500 num.defl=0
    // //% v2.shadow="range" v2.params.min=0 v2.params.max=9 v2.defl=0
    // export function Steering_gear(parameter: any, block: any): void{

    // }

/*     //####Get the data||获取数据
    //% block="Get the data" blockType="command"
    export function get_data(block: any): void{ 
        //Generator.addImport("import Robots");
        Generator.addCode(`datareturn = bytearray(5)`);
        Generator.addCode(`datareturn = Robots.Sta()`);
        //Generator.addCode(`time.sleep_ms(1000);`);
    }

    //####Get the data status||获取数据的类型
    //% block="Get the data status" blockType="reporter"
    export function get_data_status(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(["datareturn", Generator.ORDER_UNARY_POSTFIX]);

    }  */

    //###Returns action status information||返回动作组状态信息
    //% block="Returns action group status information" blockType="reporter"
    export function Action_Status(block: any): void{
        //Generator.addImport("import Robots");
		Generator.addCode(`Robots.Action_Status()`);
        //Generator.addCode(["datareturn[1]", Generator.ORDER_UNARY_POSTFIX]);
    }

    //###Return  hexadecimal number||返回状态信息
    //% block="Returns the status information of the robot itself" blockType="reporter"
    export function Status(block: any): void{
        //Generator.addImport("import Robots");
        //Generator.addCode(`if datareturn != None:`);
		Generator.addCode(`Robots.Status()`);
        //Generator.addCode(["datareturn[0]", Generator.ORDER_UNARY_POSTFIX]);
    }

    //###图传
    //% block="Image transmission[IMAGE]" blockType="command"
    //% IMAGE.shadow="normal" IMAGE.defl="image"
    export function Image_transfer(parameter: any,block:any):void{
        let image = parameter.IMAGE.code;
        Generator.addCode(`Robots.img_app(${image},50)`)
     }

    //####Ball tracking||小球[X.Y.SIXE]
    //% block="Ball_Obtain [IMAGE] threshold [THRESHOLD]" blockType="reporter"
    //% IMAGE.shadow="normal" IMAGE.defl="image"
    //% THRESHOLD.shadow="normal" THRESHOLD.defl="[37 ,75 ,15 ,47 ,4 ,40]"
    export function Ball_Obtain(parameter: any, block: any):void{
        let image = parameter.IMAGE.code;
        let threshold = parameter.THRESHOLD.code;
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.IdentifyLine(${image},${threshold})`);
    }

    //####Ball tracking||小球跟踪
    //% block="Ball_tracking [IMAGE]" blockType="command"
    //% IMAGE.shadow="normal" IMAGE.defl="[0,0,0]"
    export function Ball_Tracking(parameter: any, block: any):void{
        let image = parameter.IMAGE.code;
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.Ball_Tracking(${image})`);
    }

    //###线条信息[x]
    //% block="Line_Obtain [IMAGE] threshold [THRESHOLD]" blockType="reporter"
    //% IMAGE.shadow="normal" IMAGE.defl="image"
    //% THRESHOLD.shadow="normal" THRESHOLD.defl="[31, 54, -5, 16, -9, 24]"
    export function Line_Obtain(parameter: any,block: any): void{
        let image = parameter.IMAGE.code;
        let threshold = parameter.THRESHOLD.code;
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.Line_distinguish(${image},${threshold})`);
    }


    //###巡线
    //% block="Hunt following [IMAGE]" blockType="command"
    //% IMAGE.shadow="normal" IMAGE.defl="0"
    export function Hunt_following(parameter: any,block: any): void{
        let image = parameter.IMAGE.code;
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.Line_patrol(${image})`);
    }
	
    //###巡线
    //% block="Hunt followings [IMAGE] gait [MODE] speed [SPEED] PID Pos[Pos_P] Angle[Angle_P]" blockType="command"
    //% IMAGE.shadow="normal" IMAGE.defl="(0,0)"
	//% MODE.shadow="dropdown" MODE.options="gait" MODE.defl="gait.Trot"
	//% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=14 SPEED.defl=0
	//% Pos_P.shadow="normal" Pos_P.defl="0.00511"
	//% Angle_P.shadow="normal" Angle_P.defl="0.00211"
    export function Hunt_followings(parameter: any,block: any): void{
        let image = parameter.IMAGE.code;
		let mode = parameter.MODE.code;
		let speed = parameter.SPEED.code;
		let Pos_P = parameter.Pos_P.code;
		let Angle_P = parameter.Angle_P.code;
        Generator.addImport("import sensor");
        Generator.addCode(`Robots.Line_patrols(${image},${mode},${speed},${Pos_P},${Angle_P})`); 
    }

        //###手势初始化
    //% block="Gesture configuration settings" blockType="reporter"
    export function Gesture_init(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.i2cscan_gesture()`);
    }

    //###获取手势值
    //% block="Returns the value of the gesture direction" blockType="reporter"
    export function Get_Gesture(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.GetGesture()`);
    }

    //###选择手势值
    //% block="A value that defines the direction of the gesture[GESTURE]" blockType="reporter"
    //% GESTURE.shadow="dropdown" GESTURE.options="gesture" GESTURE.defl="gesture.1"
    export function Gesture_type(parameter: any,block: any): void{
        let gesture = parameter.GESTURE.code;
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Select_gesture_as(${gesture})`);
    }

    //###超声波测距
    //% block="Returns ultrasound data" blockType="reporter"
    export function Ultrasonic_ranging(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.ultrasonic_init(0x57)`);
    }

    //###IIC设备获取
    //% block="IIC Device obtain" blockType="reporter"
    export function IICDevice_obtain(block: any): void{
        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.IIC_Device()`);
    }

    //###Return Sta all||返回状态信息
    //% block="Returns the status information of the robot itself" blockType="reporter"
    export function Sta_all(block: any): void{
        //Generator.addImport("import Robots");
        //Generator.addCode(`if datareturn != None:`);
		Generator.addCode(`Robots.Sta()`);
        //Generator.addCode(["datareturn[0]", Generator.ORDER_UNARY_POSTFIX]);
    }
    // //###音频控制
    // //% block="Play music[CM]Song selection[DA]" blockType="command"
    // //% CM.shadow="dropdown" CM.options="CMD" CM.defl="CMD.0x03"
    // //% DA.shadow="dropdown" DA.options="dat" DA.defl="DA.0x00"
    // export function Audio_control(parameter: any, block: any): void{
    //     let CMD = parameter.CM.code;
    //     let dat = parameter.DA.code;

    //     //Generator.addImport("import Robots");
    //     Generator.addCode(`Robots.Audio_control(${CMD},0,${dat})`);
    // }

    // // //###手柄控制
    // //% block="Handle control" blockType="command"
    // export function Handle_control(block: any): void{
    //     Generator.addImport("import Robots");
    //     Generator.addCode(`Robots.Handle_control()`);
    // }
	
	//###Mechanical_Control||机械臂控制
    //% block="Mechanical_Control[ARM]Pos[Pos]Speed[Speed]Time[Time]" blockType="command"
    //% ARM.shadow="dropdown" ARM.options="ARM" LEG.defl="ARM.0"
    //% Pos.shadow="range" Pos.params.min=0 Pos.params.max=1024 Pos.defl=512
    //% Speed.shadow="range" Speed.params.min=0 Speed.params.max=1000 Speed.defl=0
    //% Time.shadow="range" Time.params.min=0 Time.params.max=1000 Time.defl=0
    export function Mechanical_Control(parameter: any, block: any): void{
        let ARM = parameter.ARM.code;
        let Pos = parameter.Pos.code;
        let Speed = parameter.Speed.code;
        let Time = parameter.Time.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Mechanical_control(${ARM},${Pos},${Speed},${Time})`);
    }
	
	//###Mechanical_Pos_control||机械臂位置控制
    //% block="Mechanical_Pos_control revolve Pos[Pos1] BOOM Pos[Pos2] Forearm Pos[Pos3] Clip Pos[Pos4]" blockType="command"
    //% Pos1.shadow="range" Pos1.params.min=0 Pos1.params.max=1024 Pos1.defl=512
    //% Pos2.shadow="range" Pos2.params.min=0 Pos2.params.max=1024 Pos2.defl=512
    //% Pos3.shadow="range" Pos3.params.min=0 Pos3.params.max=1024 Pos3.defl=512
	//% Pos4.shadow="range" Pos4.params.min=0 Pos4.params.max=1024 Pos4.defl=512
    export function Mechanical_Pos_control(parameter: any, block: any): void{

        let Pos1 = parameter.Pos1.code;
        let Pos2 = parameter.Pos2.code;
        let Pos3 = parameter.Pos3.code;
		let Pos4 = parameter.Pos4.code;

        //Generator.addImport("import Robots");
        Generator.addCode(`Robots.Mechanical_Pos_control(${Pos1},${Pos2},${Pos3},${Pos4})`);
    }
	
	//###Mechanical_Parameter||机械臂参数
    //% block="Mechanical_Parameter [ARM] Speed[Speed] Time[Time]" blockType="command"
    //% ARM.shadow="dropdown" ARM.options="ARM" LEG.defl="ARM.0"
    //% Speed.shadow="range" Speed.params.min=0 Speed.params.max=1000 Speed.defl=0
    //% Time.shadow="range" Time.params.min=0 Time.params.max=1000 Time.defl=0
    export function Mechanical_Parameter(parameter: any, block: any): void{

        let ARM = parameter.ARM.code;
        let Speed = parameter.Speed.code;
        let Time = parameter.Time.code;

        Generator.addCode(`Robots.Mechanical_Parameter(${ARM},${Speed},${Time})`);
    }
}
